name: Docker

on:
  push:
    # Publish `master` as Docker `latest` image.
    branches:
      - master

    # Publish `v1.2.3` tags as releases.
    tags:
      - v*

# TODO: Check how to build for the master branch and the latest tag
env: 
  IMAGE_NAME: RockMyy-builder
  VERSION: v1.0

jobs:
  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  poupouche:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'

    steps:
      - uses: actions/checkout@v2

      - name: Build image
        run: docker build . --file Dockerfile --build-arg=$VERSION --tag rockmyy-builder

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin

      - name: Push image
        run: |
          IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
          
          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')

          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=$VERSION

          docker tag rockmyy-builder $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION
      
      - name: Test image
        run: |
          docker-compose up &&
          tar cJvpf build.tar.xz build/* &&
          export BRANCH=$(grep KERNEL_BRANCH GetPatchAndCompileKernel.sh | head -n1 | cut -d'=' -f2) &&
          mkdir -p /tmp/archive &&
          mv build.tar.xz /tmp/archive/build-$BRANCH.tar.xz
      
      - name: Upload artifact
        uses: actions/upload-artifact@v1
        with:
          name: latest-RockMyy-build
          path: /tmp/archive
